
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Scanner;

abstract class Person{		//按照题目要求定义抽象类，其实是不是抽象类并不重要
    private String name, c;
    private int age;
    private boolean gender;

    public String getName() {
        return name;
    }

//	public void setName(String name) {
//		this.name = name;
//	}

    public String getC() {
        return c;
    }

//	public void setC(String c) {
//		this.c = c;
//	}

    public int getAge() {
        return age;
    }

//	public void setAge() {
//		this.age = age;
//	}

    public boolean isGender() {
        return gender;
    }
//
//	public void setGender(boolean gender) {
//		this.gender = gender;
//	}

    public Person(String c, String name, int age, boolean gender) {
        this.c = c;
        this.name = name;
        this.age = age;
        this.gender = gender;
    }

    @Override
    public String toString() {
        return name + "-" + age + "-" + gender;
        //返回"name-age-gender"格式的字符串
    }

    public boolean equals(Object obj) {
        Person per = (Person) obj;
        //对于String类型，如果为null则不创建对象，而赋值为null。比较name之前应先判断其是否为空。
        if(per.name == null || this.name == null)
            return false;
        return (per.name.compareTo(this.name) == 0 && per.age == this.age && per.gender == this.gender );
        //比较name、age、gender,都相同返回true,否则返回false
    }
}

class Student extends Person{
    private String stuNo;
    private String clazz;

//	public String getStuNo() {
//		return stuNo;
//	}
//
//	public void setStuNo(String stuNo) {
//		this.stuNo = stuNo;
//	}
//
//	public String getClazz() {
//		return clazz;
//	}
//
//	public void setClazz(String clazz) {
//		this.clazz = clazz;
//	}

    public Student(String c, String name, int age, boolean gender, String stuNo, String clazz) {
        super(c, name, age, gender);	//用super复用Person类的相关有参构造函数,切记放在首句
        this.stuNo = stuNo;
        this.clazz = clazz;
    }

    public String toString(){
        return super.toString() + "-" + stuNo + "-" + clazz;
        //返回 “Student:person的toString-stuNo-clazz”格式的字符串
    }

    public boolean equals(Object obj) {
        Student per = (Student)obj;
        if(super.equals(obj)) {
            //首先调用父类的equals方法,如果返回true，则继续比较stuNo与clazz。
            if(per.stuNo == null || per.clazz == null || this.stuNo == null || this.clazz == null)
                return false;
            return (per.stuNo.compareTo(this.stuNo) == 0 && per.clazz.compareTo(this.clazz) == 0);
        }
        return false;
    }
}

class Company{
    private String name;

    //	public String getName() {
//		return name;
//	}
//
//	public void setName(String name) {
//		this.name = name;
//	}
//
    public Company(String name) {
        this.name = name;
    }

    public String toString() {	//直接返回name
        return name;
    }

//	@Override
//	public int hashCode() {
//		final int prime = 31;
//		int result = 1;
//		result = prime * result + ((name == null) ? 0 : name.hashCode());
//		return result;
//	}

    //右键Source或者Alt+Shift+s;然后选择Generate hashCode() and equals()...（这两个一般要一起用的）
    @Override
    public boolean equals(Object obj) {
        if (this == obj)	//如果this和obj指向的内存单元相同，即两个指的就是同一个东西当然要返回true了
            return true;
        if (obj == null)	//如果obj指向空
            return false;
        if (getClass() != obj.getClass())	//getClass()获得类名，若类都不同那内容肯定也不同了
            return false;
        Company other = (Company) obj;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;	//name相同返回true
    }
}

class Employee extends Person{
    private Company company;
    private double salary;

//	public Company getCompany() {
//		return company;
//	}
//
//	public void setCompany(Company company) {
//		this.company = company;
//	}
//
//	public double getSalary() {
//		return salary;
//	}
//
//	public void setSalary(double salary) {
//		this.salary = salary;
//	}

    public Employee(String c, String name, int age, boolean gender, double salary, Company company) {
        super(c, name, age, gender);		//使用super复用Person类的相关有参构造函数
        this.salary = salary;
        this.company = company;
    }


    @Override
    public String toString() {
        return super.toString() + "-" + company.toString() + "-" + salary;
        //返回"Employee:person的toString-company-salary"格式的字符串
    }

    @Override
    public boolean equals(Object obj) {
        if(super.equals(obj)) {		//首先调用父类的equals方法,如果返回true。再比较company与salary。
            Employee per = (Employee)obj;
            if(this.company.toString() == null || per.company.toString() == null)
                return false;
            //比较salary属性时，使用DecimalFormat df = new DecimalFormat("#.#");保留1位小数
            String newpersalary = new DecimalFormat("#.#").format(per.salary);
            String newthissalary = new DecimalFormat("#.#").format(this.salary);
            return (per.company.toString().compareTo(this.company.toString()) == 0 && newpersalary.compareTo(newthissalary) == 0);
        }
        return false;
    }

}

public class ex06 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        String c, name, stuNo, clazz, companyname, str;
        int age;
        boolean gender;
        double salary;
        ArrayList<Person> personList = new ArrayList<Person>();
        ArrayList<Student> studentList = new ArrayList<Student>();
        ArrayList<Employee> employeeList = new ArrayList<Employee>();
        while(true) {
            c = input.next();
            if(c.compareTo("s") == 0) {
                name = input.next();
                age = input.nextInt();
                gender = input.nextBoolean();
                stuNo = input.next();
                clazz = input.next();
                Student student = new Student(c, name, age, gender, stuNo, clazz);
                personList.add(student);
            }
            else if(c.compareTo("e") == 0) {
                name = input.next();
                age = input.nextInt();
                gender = input.nextBoolean();
                salary = input.nextDouble();
                companyname = input.next();
                Company company = new Company(companyname);
                Employee employee = new Employee(c, name, age, gender, salary, company);
                personList.add(employee);
            }
            else {
                personList.sort(Comparator.comparing(Person::getName).thenComparingInt(Person::getAge));
                //对personList中的元素实现先按照姓名升序排序，姓名相同再按照年龄升序排序。
                for(int i = 0; i < personList.size(); ++i) {
                    //将personList中元素输出，并将其非重复元素分到studentList和employeeList数组中
                    if(personList.get(i).getC().compareTo("s") == 0) {
                        System.out.println("Student:" + personList.get(i).toString());
                        int flag = 0;
                        for(int j = 0; j < studentList.size(); ++j) {
                            if(studentList.get(j).equals(personList.get(i))) {
                                flag = 1;
                                break;
                            }
                        }
                        if(flag == 0)
                            studentList.add((Student)personList.get(i));
                    }
                    else if(personList.get(i).getC().compareTo("e") == 0){
                        System.out.println("Employee:" + personList.get(i).toString());
                        int flag = 0;
                        for(int j = 0; j < employeeList.size(); ++j) {
                            if(employeeList.get(j).equals(personList.get(i))) {
                                flag = 1;
                                break;
                            }
                        }
                        if(flag == 0)
                            employeeList.add((Employee)personList.get(i));
                    }
                }
                str = input.next();
                //如果输入为exit则return退出程序
                if(str.compareTo("exit") == 0 || str.compareTo("return") == 0)
                    return;
                System.out.println("stuList");
                for(int i = 0; i < studentList.size(); ++i)
                    System.out.println("Student:" + studentList.get(i).toString());
                System.out.println("empList");
                for(int i = 0; i < employeeList.size(); ++i)
                    System.out.println("Employee:" + employeeList.get(i).toString());
            }
        }
    }
}

